<?php

namespace app\admin\controller;

use app\Request as R;
use app\admin\Validate;
use app\admin\model\AdminUser as Model;
use app\admin\model\Role;
use app\Exception;
use app\admin\service\AdminUserManger;
use app\admin\service\RoleManger;

class AdminUser extends BaseController{


    function index(R $req)
    {
        $page = $req->param('page/i');
        $page = $page<1?1:$page;
        // $datalist = Model::order(Model::PK,'desc')->paginate(config('app.admin_page_size'));
        return view("/admin_user/index",[
            'datalist'=>app(AdminUserManger::class)->query([
                'list_rows'=>config('app.admin_page_size'),
                'page'=>$page
            ])
        ]);
    }

    function delete(R $req)
    {
        $id = $req->param('id/i',0);
        if($id == 1){
            return error_json("不允许删除创建者");
            
        }
        app(AdminUserManger::class)->delete($id);
        return success_json("删除成功");
    }
    function add(R $req)
    {
        if($req->isPost()){
            try{
                $data = $req->only(['username','email','role_id','enable','password','name']);
                app(AdminUserManger::class)->insert($data);
                return success_json("添加成功");
            }catch(Exception $e){
                return error_json($e->getMessage());
            }
        }
        return view("/admin_user/add",[
            'roles'=>app(RoleManger::class)->all()
        ]);
    }

    function edit(R $req)
    {

        $user = app(AdminUserManger::class)->find($req->route('id/i',0));
        if(!$user){
            return error("找不到该用户");
        }elseif($user['id'] == 1){
            return error("不允许修改创建者");
        }
        if($req->isPost()){
            try{
                $data = $req->only(['username','email','role_id','enable','password','name']);
                app(AdminUserManger::class)->edit($user['id'],$data);
                return success_json("修改成功");
            }catch(Exception $e){
                return error_json($e->getMessage());
            }
        }
        return view("/admin_user/update",['model'=>$user,
            'roles'=>app(\app\admin\service\RoleManger::class)->all()
        ]);
    }
    function update_password(R $req)
    {
        /** @var Model */
        $user = $this->getAdminUserService()->find($req->route('id'));
        if(!$user){
            return error("找不到该用户");
        }elseif($user['id'] == 1){
            return error("不允许修改创建者");
        }

        if($req->isPost()){
            try{
                $password = $req->post('new_password');
                app(AdminUserManger::class)->update($user['id'],[
                    'password'=>$password
                ]);
                return success_json("修改成功");
            }catch(Exception $e){
                return error_json($e->getMessage());
            }
        }
        
        return view('/admin_user/update_password',['user'=>$user]);
    }


    /**
     * @return AdminUserManger
     */
    protected  function getAdminUserService()
    {
        return app(AdminUserManger::class);
    }

    /**
     * @return \app\admin\service\RoleManger
     */
    protected function getRoleService()
    {
        return app(\app\admin\service\RoleManger::class);
    }
}